package com.aiyige.im.model.viewHolder;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.im.model.CustomMsgData;
import com.aiyige.im.model.attachment.SubjectAttachment;
import com.aiyige.model.Subject;
import com.aiyige.utils.GlideUtil;
import com.netease.nim.uikit.business.session.viewholder.MsgViewHolderBase;
import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;

public class MsgViewHolderSubject extends MsgViewHolderBase {
    private ImageView ivPlayIcon;
    private ImageView ivCover;
    private TextView tvTitle;
    private LinearLayout llPrice;
    private TextView tvPrice;
    private TextView tvLineationPrice;
    private TextView tvOther;

    private SubjectAttachment msgAttachment;

    public MsgViewHolderSubject(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.nim_item_subject_content;
    }

    @Override
    protected void inflateContentView() {
        ivPlayIcon = (ImageView) view.findViewById(R.id.nim_iv_play);
        ivCover = (ImageView) view.findViewById(R.id.nim_iv_cover);
        tvTitle = (TextView) view.findViewById(R.id.nim_tv_title);
        llPrice = view.findViewById(R.id.nim_ll_price);
        tvPrice = (TextView) view.findViewById(R.id.nim_tv_price);
        tvLineationPrice = (TextView) view.findViewById(R.id.nim_tv_lineation_price);
        tvOther = (TextView) view.findViewById(R.id.nim_tv_other);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void bindContentView() {
        msgAttachment = (SubjectAttachment) message.getAttachment();
        CustomMsgData data = msgAttachment.getKeyData();
        if (data != null) {
            GlideUtil.with(context).loadCover(data.getImage()).into(ivCover);
            if (!TextUtils.isEmpty(data.getSubject()) && (data.getSubject().equals(Subject.video) || data.getSubject().equals(Subject.video_course))) {
                ivPlayIcon.setVisibility(View.VISIBLE);
            } else {
                ivPlayIcon.setVisibility(View.INVISIBLE);
            }
            tvTitle.setText(data.getTitle());
            tvOther.setText(data.getSubtitle());
            if (!TextUtils.isEmpty(data.getSubject()) && (data.getSubject().equals(Subject.video_course) || data.getSubject().equals(Subject.major_course))) {
                llPrice.setVisibility(View.VISIBLE);

                if (!TextUtils.isEmpty(data.getLinePrice())) {
                    tvLineationPrice.setVisibility(View.VISIBLE);
                    tvLineationPrice.setText("￥" + data.getLinePrice());
                } else {
                    tvLineationPrice.setVisibility(View.GONE);
                }
                tvPrice.setText(data.getPrice());
            } else {
                tvLineationPrice.setVisibility(View.GONE);
                llPrice.setVisibility(View.GONE);
                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                tvOther.setLayoutParams(rlp);
            }
        }
    }

    @Override
    protected void onItemClick() {
        msgAttachment = (SubjectAttachment) message.getAttachment();
        CustomMsgData data = msgAttachment.getKeyData();
        if (data != null && !TextUtils.isEmpty(data.getRouter())) {
            Router.start(data.getRouter());
        }
    }

    @Override
    protected int leftBackground() {
        return R.drawable.item_custom_msg_selector;
    }

    @Override
    protected int rightBackground() {
        return R.drawable.item_custom_msg_selector;
    }
}